<?php


namespace generate\field;

class LayuiFile extends Field
{
    public static $html = <<<EOF
  
    <div class="form-group">
    <label for="file" class="col-sm-2 control-label">文件</label>
    <div class="col-sm-8">
        <div class="layui-inline">
            <div class="layui-upload-drag" id="[FIELD_NAME]">
                <i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>
            </div>
        </div>

        <table  class="layui-table file-list {if isset(\$data) && !empty(\$data['[FIELD_NAME]']) && \$data['[FIELD_NAME]'] != ''}  {else}layui-hide {/if}"
            id="[FIELD_NAME]-list">
            <tr>
                <td>名称</td>
                <td>大小</td>
                <td>类型</td>
                <td>状态</td>
                <td>操作</td>

            </tr>
            {if isset(\$data) && !empty(\$data['[FIELD_NAME]']) && \$data['[FIELD_NAME]'] != ''}
            {foreach \$data['[FIELD_NAME]'] as \$key => \$vo}
            <tr id='upload-{\$key}'>
                <td>{\$vo.original_name|default=''}<input type='hidden' value="{\$vo|arr_encode}"
                        name='[FIELD_NAME][]' id='[FIELD_NAME]_{\$key}'></td>
                <td>{\$vo.size|default=''}</td>
                <td>{\$vo.type|default=''}</td>
                <td> 已上传</td>
                <td>
                    <button
                        class='layui-btn layui-mini layui-bg-cyan file-upper {if \$key == 0} layui-hide {/if}'
                        type='button'>上移</button>
                    <button
                        class='layui-btn layui-mini layui-bg-blue file-lower {if count(\$data["[FIELD_NAME]"])== (\$key+1)} layui-hide {/if}'
                        type='button'>下移</button>
                    <button class='layui-btn layui-mini layui-btn-danger file-del'
                        type='button'>删除</button>
                </td>
            </tr>
            {/foreach}
            {/if}
        </table>
        <button type="button" id="[FIELD_NAME]-upload" class="layui-btn all-upload layui-hide">全部上传</button>
    </div>

</div>

<script>
    $(function () {
        layui.config({
            base: '/static/admin/plugins/layFile/' //layui自定义layui组件目录
        }).use(['layFile'], function () {
            var $ = layui.jquery,
                layFile = layui.layFile,
                layer = layui.layer;
            //多文件上传
            layFile.render({
                elem: '#[FIELD_NAME]',
                bindAction:'#[FIELD_NAME]-upload',//全部上传按钮
                tableList:'#[FIELD_NAME]-list', //表格list
                url: "{:url('upfile/upload')}" ,// 文件上传地址
                field: '[FIELD_NAME]',
                size:1024*2, //文件上传大小
                exts:'jpg|png|gif|bmp|svg|jpeg|mp4|doc|docx|pdf|xls|xlsx|ppt|pptx|txt|rar|zip'//允许上传的文件后缀
            });


        });
    })
</script>


\n
EOF;


 

    //多图上传模型内修改器与获取器
    public static $modelAttrCode = <<<EOF
                //[FORM_NAME]获取器
                public function get[FIELD_NAME]Attr(\$value)
                {
                    \$info = json_decode(\$value, true);
                    \$data = [];
                    foreach (\$info as \$vo) {
                        \$data[] = json_decode(\$vo, true);
                    }
                    return \$data;
                }

                //[FORM_NAME]修改器
                public function set[FIELD_NAME]Attr(\$value)
                {
                    foreach(\$value as \$key => \$vo){
                        if(\$vo == '' || empty(\$vo)){
                            unset(\$value[\$key]);
                        }
                    }
                    return json_encode(\$value);
                }

\n
EOF;


    public static $rules = [
        'required'   => '非空',
        'file_size'  => '文件大小限制',
        'file_image' => '图片类型',
        'regular'    => '自定义正则'
    ];


    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace('[FORM_NAME]', $data['form_name'], $html);
        $html = str_replace('[FIELD_NAME]', $data['field_name'], $html);
        return $html;
    }
}
